<?php
if(!defined('BLOG'))exit('不作死就不会死');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/1/2
 * Time: 14:19
 */
base::load_class('Smarty');
class index{
    private $db,$smarty;
    public function __construct(){
        $this->db = base::load_model('content_model');
        $this->smarty = new Smarty(base::load_config('system'));
        $this->smarty->registerPlugin("modifier","mysubstr", "smarty_modifier_mysubstr");
        $categorys_db = base::load_model('navigation_model');
        $categorys = $categorys_db->selectCached();
        $this->smarty->assign('categorys',$categorys);
        $this->smarty->assign('hots',$this->hots());
        $this->smarty->assign('tags',$this->tags());
       
    }
    public function init(){
        $forminfo  = my_addslashes($_GET);
        $where =  isset($forminfo['category']) == ''?'':' where typeid = '.$forminfo['category'];
        $articles = $this->db->selectCached('*',$where,'order by id desc',$limit='limit 0,5');
        $this->smarty->assign("articles",$articles);
        $this->smarty->display('index.html');
    }
    public function show(){
        $forminfo  = my_addslashes($_GET);
        $aid = isset($forminfo['aid'])?$forminfo['aid']:'';
        $where = 'where id = '.$aid;
        $article = $this->db->selectCached('*',$where);
        $this->comment_db = base::load_model('comment_model');
        $comm_where = 'where article_id = '.$aid;
        $comms = $this->comment_db->select('*',$comm_where);
        $commnents = createtree($comms);
        $article = empty($article)? '':$article[0];
        $this->smarty->assign('article',$article);
        $this->smarty->assign('comment',$commnents);
        $this->smarty->display('article.html');

    }
    public function ajax_init(){
        $forminfo = my_addslashes($_GET);
        $aid = isset($forminfo['aid'])?$forminfo['aid']:0;
        $categoryid = isset($forminfo['category'])?$forminfo['category']:'';
        $limit = 'limit 0,5';
        $wheretype = $categoryid == ''?'':' and typeid = '.$categoryid;
        $where = 'where id < '.$aid.$wheretype;
        $articles = $this->db->selectCached($data = '*',$where,'',$limit);
        $array = array();
        foreach ($articles as $val){
            $array[][]='<li><div class="desc"><div style="width:700px;" class="addfile">'.''.'</div><div class="clrear"></div><div><h2><!-- <span>PIC</span> -->'.$val['title'].'</h2><p>'.smarty_modifier_mysubstr(strip_tags($val['content']),600).'</p><small>By<a href="#"> '.$val['author'].' </a>在  '.date('Y-h-m',$val['leavetime']).' <a href="">评论</a>: '.$val['comm'].' </small><p class="readmore"><a href="'.WEB_PATH.'index.php?m=content&c=index&a=show&category='.$val['typeid'].'&aid='.$val['id'].'" data-id="'.$val['id'].'" data-category="'.$val['typeid'].'">Read More</a></p></div></div></li>';
        }
        echo json_encode($array);
    }
    private function hots(){
        $order = 'order by click desc';
        $hots = $this->db->selectCached($data = '*','',$order,'limit 0,5');
        return $hots;
    }

    private function tags(){
        $order = 'order by click asc';
        $tags = $this->db->selectCached(array('id,tag,typeid'),'',$order,'limit 0,16');

        foreach($tags as $key => $tag){
            $array = explode(',',$tag['tag']);
            $tags[$key]['tag'] = $array[0];
        }
        return $tags;
    }
}

